<?php
include_once 'field.class.php';

class field_department extends field
{
    public $error;
    protected $fieldname = '部门';

    /**
     * 生成设置项
     * @author menghao
     * @param <string> $data
     * @return <string>
     */
    function settings($data='')
    {
        $data = unserialize($data);
        $data['width'] = isset($data['width']) && is_numeric ($data['width']) ? $data['width'] : 300;
        $data['height'] = isset($data['height']) && is_numeric ($data['height']) ? $data['height'] : 200;

        $setting = '';
        $setting .= '<label>多选列表宽度：'.form::text('settings[width]', 'setting_width', $data['width'], 7).'</label> ';
        $setting .= '<label>多选列表高度：'.form::text('settings[height]', 'setting_height', $data['height'], 7).'</label> ';
        return $setting;
    }

    /**
     * 检查设置项数据
     * @author menghao
     * @param <array> $data
     */
    function check_settings($data)
    {
        if(!is_numeric($data['width']))
        {
            $this->error = "多选列表宽度必须是数字";
            return false;
        }

        if(!is_numeric($data['height']))
        {
            $this->error = "多选列表高度必须是数字";
            return false;
        }
        return true;
    }

    function get_data($data)
    {
        $data = implode(',', $data);
        return $data;
    }

    function get_dept_list()
    {
        if(!class_exists('type'))
        {
            include 'type.class.php';
        }
        $type = new type;
        $type_code = 'department';
        $data = $type->get_typs($type_code);
        $dept_list = array();
        foreach ($data as $r)
        {
            if($r['parentcode'] == $type_code)
            {
                $dept_list[$r['tcode']]['groupname'] = $r['tname'];
            }
            else
            {
                $dept_list[$r['parentcode']]['groupchild'][] = array('id'=>$r['tcode'],'name'=>$r['tname']);
            }
        }
        return $dept_list;
    }

    function create_form($value='')
    {
        $field = $this->field_data['field'];
        $width = $this->field_data['settings']['width'];
        $height = $this->field_data['settings']['height'];

        $dept_list = $this->get_dept_list();
        $value = explode(',', $value);
        $form = form::multi_select_group($dept_list, "data[$field][]", $field, $value,
                '--------------', "width:{$width}px;height:{$height}px;");
        return $form;
    }
}
?>